<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
 * Description of category_model
 *
 * @author Subha Sundar Das (subhasundar.das@gmail.com)
 */
class Admin_news_model extends CI_Model {

	//put your code here


	function __construct() {
		parent::__construct();
		log_message('debug', 'Init Model Initialized');
	}
	
	
	//------------Get All News()----------
	function get_all_news($num=NULL, $offset=NULL, $featured=0) {
		$subcond = "where status=51 and featured=$featured";
		$limit = "";
		if (isset($num) && isset($offset))
			$limit = " order by (id) desc limit $offset, $num";
		$sql = "select * from news " . $subcond . $limit;
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	
	function get_news_report($featured=false, $status=1,$num=NULL, $offset=NULL) {
		$subcond = "where status=$status";
		$limit = "";
		if (isset($num) && isset($offset))
			$limit = " order by (id) desc limit $offset, $num";
		$sql = "select * from news " . $subcond . $limit;
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	
	function get_inactive_news_report($featured=false, $status=51,$num=NULL, $offset=NULL) {
		$subcond = "where status=$status";
		$limit = "";
		if (isset($num) && isset($offset))
			$limit = " order by (id) desc limit $offset, $num";
		$sql = "select * from news " . $subcond . $limit;
		$query = $this->db->query($sql);
		$result = $query->result();
		return $result;
	}
	
	//-- Get listings(Multi Row) --//
	/*function get_all_news($featured = false, $status = 1, $no = NULL, $offset = NULL) {
		
			if ($featured == false) {
				$sql = "Select * from news where status=$status order by id desc limit $no ";
			} else {
				$sql = "Select * from news where featured =1 and status=$status order by id desc limit $no ";
			}
			$query = $this->db->query($sql);
			return $query->result();
		
	}
	*/
	function search($search=null){
		$sql = "Select * from news where title like '$search%' order by id desc";
		$query = $this->db->query($sql);
		$result = $query->result();
		if($result){
			return $query->result();
		}else{
			return false;
		}
	
	
	}
	
	function get_news($id=NULL)
	{
		$sql="select * from news where id=$id and status=51";
		$query=$this->db->query($sql);
		$row=$query->row();
		return $row;
	}
	
	//------------Insert News(27.9.2012)----------
	function insert_news($title=NULL, $detail=NULL, $meta_title=NULL, $meta_keyword=NULL, $meta_description=NULL) {
		$date = date('d-M-Y');
		$sql = "insert into news (title,description,date,meta_title,meta_keyword,meta_description) values('$title','$detail','$date','$meta_title','$meta_keyword','$meta_description')";
		$this->db->query($sql);
	}
	
	//------------Update News(27.9.2012)----------
	function update_news($id=NULL, $title=NULL, $detail=NULL, $meta_title=NULL, $meta_keyword=NULL, $meta_description=NULL) {
		$date = date('d-M-Y');
		$sql = "update news set `title`='$title',`description`='$detail',meta_title='$meta_title',meta_description='$meta_keyword',meta_description='$meta_description',`updated_on`='$date' where id='$id'";
		$this->db->query($sql);
	}
	
	//------------Delete News(27.9.2012)----------
	function delete_news($id=NULL) {
		$sql = "delete from news where id=$id";
		$this->db->query($sql);
	}
	
	function change_status($status=NULL, $id=NULL)
	{
		$sql="update news set status=$status where id=$id";
		$this->db->query($sql);
		return true;
	}
	
	function change_news_status($id, $status) {
		if ($status == 1) {
			$sql = "update news set status=51 where id=$id";
		}
		if ($status == 51) {
			$sql = "update news set status=1 where id=$id";
		}
		$this->db->query($sql);
	}
}